<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

Class UserController Extends baseController {
	
	private $numRows= 5;	

    public function index($currentUser, $id = null) 
    {   
    	if(isset($_GET['numRows'])){
    		if($_GET['numRows'] > 0) {
    			$this->numRows = $_GET['numRows']; 
    		}
    	}
    	if ($id == 'all'){
    		$id = 1;
    	}
    	$rows = '';
    	$end = $id*$this->numRows;
    	$start = ($id-1)*$this->numRows;
    	$max = $this->registry->db->get('userManager')->getNum();
	    if (($start + $this->numRows) > $max){
	    	$num = $max - $start;
	    } else {
	    	$num = $this->numRows;
	    }
    	$objArray = $this->registry->db->get('userManager')->getSome($start, $num);    	
    	$numPages = intval($max/$this->numRows) + 1;
    	
    	//$currentUser = $this->registry->db->get('userManager')->getCurrentUser();
    	
    	for($i=0; $i<count($objArray); $i++)
    	{
    		if( $i % 2 == 0){    			
    			$class = '';
    		} else {    			
    			$class = ' class="alternate-row"';
    		}
    		$u = $objArray[$i];
    		$row = '<tr'.$class.'>'.
							'<td>'.$u->get("uid").'</td>
							<td>'.$u->get("username").'</td>
							<td><a href="mailto:'.$u->get('email').'">'.$u->get('email').'</a></td>
							<td>'.$u->get('type').'</td>					
							<td class="options-width">';
    		if($u->get('type') == "Admin") {
    			$deleteLink = '<a href="../modify/'.$u->get('uid').'?numRows='.$this->numRows.'" title="Edit" class="icon-1 info-tooltip"></a>';;
    		} else {
    			$deleteLink = '<a href="../modify/'.$u->get('uid').'?numRows='.$this->numRows.'" title="Edit" class="icon-1 info-tooltip"></a>'.'<a href="../delete/'.$u->get('uid').'?numRows='.$this->numRows.'" title="Delete" class="icon-2 info-tooltip"></a>';
    		}					
    		$row = $row.$deleteLink.											
							'</td>
						</tr>';
    		$rows = $rows.$row;
    	}
    	$farLeft = '../view/1?numRows='.$this->numRows;
    	$farRight = '../view/'.$numPages.'?numRows='.$this->numRows;
    	$left = '../view/';
    	$right = '../view/';
    	if ($id == 1){
    		$left = ''.'?numRows='.$this->numRows;
    	} else {
    		$left = $left.($id-1).'?numRows='.$this->numRows;
    	}
    	if ($id == $numPages){
    		$right = ''.'?numRows='.$this->numRows;
    	} else {
    		$right = $right.($id+1).'?numRows='.$this->numRows;
    	}
    	$tags = array(
    		'rows'	=>	$rows,
    		'page'	=>	$id,
    		'numPages'	=>	$numPages,
    		'far-left'	=> $farLeft,
    		'far-right'	=> $farRight,
    		'left'	=> $left,
    		'right'	=> $right,
    	);  	    	
    	$content = $this->registry->template->getTemplate('vuser', $tags, 'Module');
        $this->registry->template->setMenuKey('user');
    	echo $this->registry->template->getPageContent('index', 'BSTO ACP - User Management', $content, array('bstoContentTitle'=>'List of All Users'));
    	
    }
    public function delete($currentUser, $id = null) 
    {
    	if ($id) {
    		//$currentUser = $this->registry->db->get('userManager')->getCurrentUser();
    		if ($id != $currentUser->get('uid')){
    			$this->registry->db->get('userManager')->delete($id);
    		}    		
    		header("Location: ../view/all?numRows=".$_GET['numRows']);
    	}
    }
	public function checkokie($missing){
   		if($missing['username']!=""){
    		return false;
    	}
   		if($missing['password']!=""){
    		return false;
    	}
    	if($missing['email']!=""){
    		return false;
    	}
    	return true;
    }
    public function modify($currentUser, $id = null) 
    {
    	if ($id) {
			//if ($id != $currentUser->get('uid')){
    		//$currentUser = $this->registry->db->get('userManager')->getCurrentUser();
				$this->registry->db->startConnection();
				$rows= mysql_query("Select * from user where uid =$id");
				$this->registry->db->closeConnection();
				$row=mysql_fetch_array($rows);
				$oldusername = $row['username'];
				$oldpassword = $row['password'];
				$oldemail = $row['email'];
				$oldcompany = $row['company'];
				$oldphone = $row['phone'];
				$oldaddress = $row['address'];
				$status1value = $row['status'];
				$status2value =  1- $status1value;
				$type1value = $row['type'];
				$type2value = ($type1value =="Admin")?"Normal":"Admin";
				$status1 = "";
				$status2 = "";
				if($status1value==0){
					$status1 ="Block";
					$status2 ="Active";
				}	
				else{
					$status1 ="Active";
					$status2 ="Block";
				}
				$tags = array(
					"username"=>"",
					"password"=>"",
					"email"=>"",
					"phone"=>"",
					"company"=>"",
					"address"=>"",
					"status1"=> $status1,
					"status2"=> $status2,
					"type1value"=> $type1value,
					"type2value"=> $type2value,
					'usernamevalue'=> $oldusername,
					'passwordvalue'=> "dongdepchai",
					'emailvalue'=> $oldemail,
					'phonevalue'=> $oldphone,
					'companyvalue'=> $oldcompany,
					'addressvalue'=> $oldaddress,
					'status1value'=> $status1value,
					'status2value'=> $status2value,
					'bstoContentTitle' => "Modify User' Information"
					
				);
				$method = $_SERVER['REQUEST_METHOD'];
				if(isset($_POST['submit'])){
					foreach($_POST as $key => $value){
						if(trim($value)=="" && ($key=="password"|| $key =="email" )){
								$tags[$key]="This field is requied.";
						}			
					}
				}
				$check = $this->checkokie($tags);
				
				$content =$this->registry->template->getModule('auser',$tags);
				$this->registry->template->setMenuKey('modifyuser');
				$output = $this->registry->template->getPageContent('index', 'BSTO ACP - Modify User Information', $content, $tags);        
				if($method=="POST" && isset($_POST['submit']) && $check){
					$username = $oldusername;
					$password = $_POST['password'];
					$email = $_POST['email'];
					$phone = $_POST['phone'];
					$company = $_POST['company'];
					$address = $_POST['address'];
					$type = $_POST['type'];
					$status = $_POST['status']; 
					$user = new User($this->registry->db);
					$user->set('uid', $id);
					if($password == "dongdepchai"){
						$user->initWithoutHash($username, $oldpassword, $email, $phone, $company, $address,$status,$type);
					}else{
						$user->init($username, $password, $email, $phone, $company, $address,$status,$type);
					}
					$user->save();
					header("Location: ../view/all");
					
				}
				echo $output; 
			//}
				
    		    		
    		//header("Location: http://google.com.vn");
    	}
    }
}
?>
